using System;
using System.Security.Policy;
using NUnit.Framework;

namespace Crawler.Tests
{
	/// <summary>
	/// tests the fetcher class
	/// </summary>
	[TestFixture]
	public class TestFetcher
	{
		Fetcher fetcher;
		string path;
		string destinationPath;
		string sourcePath;
		ResourcePath resourcePath;
		UrlStack urlStack ;
		private ICentralFactory factory;
		IDownloadStore downloadStore;
		String dummy;
		private CrawlerContext crawlerContext;

		[SetUp]
		public void Initialize()
		{
			factory = CentralFactory.centralFactory ;  	
			path = @"file:///c:/testdata/3.2 - Planning An Approach.htm";
            destinationPath = @"e:/testdata";
			sourcePath = @"file:///c:/testdata/my really awesome tree.exe";
			this.crawlerContext = new CrawlerContext() ;
			this.crawlerContext.UrlStackEmpty = true; 
			urlStack = new UrlStack(this.crawlerContext );
			downloadStore = new DownloadStore(this.crawlerContext );
			factory.Add(urlStack) ;
			factory.AddCachePath(new ResourcePath(destinationPath));
			factory.Add(downloadStore) ;
			fetcher = new Fetcher(factory, this.crawlerContext );
			resourcePath = new ResourcePath(path) ;
			urlStack.UrlAddedToUrlStack += new UrlAddedToUrlStackHandler(fetcher.NotifyFetcherAboutUrlAdditionToUrlStack ) ;
		}

	
	
		[Test]
	    public void TestFetchDumpableAndAddToDownloadStore()
		{
            urlStack.Add(new Url(path), true);
			Uri site = new Uri(path) ;
            IKey key = new Key(site.AbsoluteUri.ToLower());  
			String data = downloadStore.GetResource(key).StreamDump();
			Assertion.AssertNotNull(data);   
			Assertion.AssertEquals("",1,downloadStore.Count ) ;
			Assertion.AssertEquals("",0,urlStack.Count) ;
		}

		[Test]
		public void TestFetchNonDumpableAndAddToDownloadStore() {
			urlStack.Add(new Url(sourcePath), true);
			IKey key = new Key(sourcePath); 
			String data = downloadStore.GetResource(key).StreamDump();
			Assertion.AssertNotNull(data) ;
			Assertion.AssertEquals("",1,downloadStore.Count ) ;
			Assertion.AssertEquals("",0,urlStack.Count) ;
		}
	}
}
